import router from '@/router';
export default {
  state() {
    return {
      dynamicTags: [{ title: "首页", name: "/Index/IndexAbout" }]
    }
  },
  mutations: {
    // 添加路由--添加标签
    tagRouteS(state, val) {
      console.log(val, "添加标签666");

      if (val.name == "Index" || val.name == "Indirective-chlid" || val.name == "Login") {
        return;
      }
      //去重
      if (state.dynamicTags.find(item => item.name == val.path)) return
// 点击商品添加或者编辑标签都放在商品列表后面
      let bool = state.dynamicTags.find(item => item.name == '/Index/CategoryManagement/ProductList')//这个是在page.js页面
      if (val.meta.parent && bool) {
        let index = state.dynamicTags.findIndex(item => item.name == val.meta.parent)//找到它父亲的路径
        state.dynamicTags.splice(index + 1, 0, { title: val.meta.title, name: val.path })//把你要放的对象放到父亲后面
      } else {
        state.dynamicTags.push({ title: val.meta.title, name: val.path });
      }
    },
    //移出标签
    handleClose(state, index) {
      let { path } = router.currentRoute.value;//获取路由
      if (path == state.dynamicTags[index].name) {
        // 后面一个标签或者前面一个标签
        const { title, name } = state.dynamicTags[index + 1] || state.dynamicTags[index - 1];
        router.push({ path: name });
      }
      state.dynamicTags.splice(index, 1); // 删除当前标签
    },
    // 点击标签
    tagClick(state, name) {
      router.push(name); // 跳转到当前路径
    },
    // 下拉框的事件
    tagRefresh(state, e) {
      router.push({ path: "/Index/Indirective" + e.path });
    },
    // 关闭右边标签
    rightClose(state, e) {
      state.dynamicTags.splice(e.index + 1, state.dynamicTags.length + 1 - e.index);
      router.push(state.dynamicTags[e.index].name); // 跳转到当前页面路径
    },
    // 关闭全部标签
    allClose(state) {
      state.dynamicTags.splice(1, state.dynamicTags.length - 1);
      router.push("/Index/IndexAbout"); // 跳转到首页路径
    },
    dropChang(state) {
      document.body.click()//点击页面的任何地方都可以隐藏其它的下拉框
    }
  },
  actions: {
    // 添加路由--添加标签
    tagRouteS(contest, val) {
      contest.commit('tagRouteS', val)
    },
    //移出标签
    handleClose(contest, index) {

      contest.commit('handleClose', index)
    },
    // 点击标签跳转到当前路径
    tagClick(contest, name, index) {
      contest.commit('tagClick', name, index)
    },
    // 刷新
    tagRefresh(contest, e) {
      contest.commit('tagRefresh', e)
    },
    // 关闭右边标签
    rightClose(contest, e) {
      contest.commit('rightClose', e)
    },
    // 关闭全部标签
    allClose(contest) {
      contest.commit('allClose')
    },
    // 点击下拉框展开隐藏
    dropChang(contest) {
      contest.commit('dropChang')
    },
  },
  getters: {
    dynamicTags(state) {
      return state.dynamicTags
    }
  },
}
